﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebApplication1
{
    public partial class BooksManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserInfo"] == null || ((Entity.UserInfo)Session["UserInfo"]).UserType != "admin")
                {
                    Response.Redirect("Default.aspx");
                }

                DataTable dt = new DataTable();
                SQLHelper.getBook(out dt);
                gdvBooks.DataSource = dt;
                gdvBooks.DataKeyNames = new string[] { "Id" };
                gdvBooks.DataBind();
                Session.Remove("BookId");

            }
        }

        protected void gdvBooks_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = Convert.ToInt32(gdvBooks.DataKeys[e.RowIndex].Value);
            SQLHelper.deleteBookById(i);
            Response.Redirect("BooksManage.aspx");
        }

        protected void gdvBooks_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int i = Convert.ToInt32(gdvBooks.DataKeys[e.NewEditIndex].Value);
            Session.Add("BookId", i);
            Response.Redirect("BookEdit.aspx");
        }

        protected void gdvBooks_RowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[1].Visible = false;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Session.Remove("BookId");
            Response.Redirect("BookEdit.aspx");
        }
    }
}